#include "Map.h"
#include "Resource.h"
#include "Land.h"
#include "Enemy.h"
#include "Goomba.h"
#include "Background.h"
#include "Cloud.h"
#include "Grass.h"
#include "Brick.h"
#include "Mountain.h"
#include "Stone.h"
#include "Pipe.h"
#include "Coin.h"
#include "Star.h"
#include "LifeMushroom.h"
#include "Pirhana.h"
#include "Koopa.h"
#include "BrickEx.h"
#include "PowerUp.h"
#include "BrickQuestion.h"
#include "CheckPoint.h"
/*


Obj				ID		save
mario		:	2

goomba		:	11		v
turtle		:	12		v

brick		:	21		v
land		:	22		v
stone		:	23		v
pipe		:	24		v
question brick	:	25	V
brick hidden	:	26	V
brick 5 coin	:	27	v
brick star	: 	28		V

cloud		:	31		v
grass		:	32		v
mountain	:	33		v

coin		:	41		v
pirhana		:	42		v
lifemushroom	:	43	v
star		:	44		v
powerup		: 	45		v
coin hidden	: 	46		v
5 coin hidden	: 	47	v

checkpoint	:	51

*/




Map::Map(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, MarioManager* game_Mario)
{
	_spriteHandler = SpriteHandler;
	_d3ddv = d3ddv;
	_backBuffer = BackBuffer;

	isSubMap = false;
	isLoadSubMap = false;
	saveMario = new Mario(_d3ddv, _backBuffer, _spriteHandler, 0, 0, game_Mario, MS_NORMAL, 0);
}

void Map::LoadMapFromFile(char* fileName)
{
	string data;
	ifstream fileStream(fileName);
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			data += fileStream.get();
		}
	}
	vector<string> dataSplit = split(data, '\n');
	string temp = dataSplit[0];
	vector<string> temp_vector = split(temp, ' ');
	width_map =  atoi(temp_vector[0].c_str());
	height_map =  atoi(temp_vector[1].c_str());
	// Da co width va height
	// Cap phat mang
	mang_2_c = (int**)malloc(height_map * sizeof(int*));
	for (int i = 0; i < height_map; i++)
		mang_2_c[i] = (int*)malloc(width_map * sizeof(int));
	for (int i = 0; i < height_map; i++)
	{
		vector<string> temp_splip = split(dataSplit[i + 1], ' ');
		for (int j = 0; j < width_map; j++)
		{
			mang_2_c[i][j] = atoi(temp_splip[j].c_str());
		}
	}
	// Luu mang Ok
}

std::vector<std::string>& Map::split(const std::string &s, char delim, std::vector<std::string> &elems)
{
	std::stringstream ss(s);
	std::string item;
	while(std::getline(ss, item, delim))
	{
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> Map::split(const std::string &s, char delim)
{
	std::vector<std::string> elems;
	return split(s, delim, elems);
}

vector<Object*> Map::GetObjects()
{
	vector<Object*> ans;
	for (int i = 0; i < height_map; i++)
		for (int j = 0; j < width_map; j++)
		{
			
			if(mang_2_c[i][j] == 1)
			{
				CLand *cland = new CLand(_d3ddv, _backBuffer, _spriteHandler, j * 25.0f , i * 25.0f + 300);
				//cland->Init(_d3ddv, _backBuffer, _spriteHandler);
				ans.push_back(cland);
			}
			if(mang_2_c[i][j] == 4)
			{
				Enemy *enemy = new Enemy(j * 50.0f , i * 50.0f + 300);
				//enemy->Init(_d3ddv, _backBuffer, _spriteHandler);
				ans.push_back(enemy);
			}
		}
		return ans;
}

vector<Object*> Map::GetAllObjects(QuadTree *quadTree, MarioManager* game_Mario)
{
	Object *obj;
	bool isStatic;
	//quadTree = new QuadTree(0,0,720,0);

	vector<Object*> ans;
	for (int i = 0; i < height_map; i++)
		for (int j = 0; j < width_map; j++)
		{
			obj = NULL;
			if(mang_2_c[i][j] == 31)
			{
				obj = new CLoud(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(31);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 32)
			{
				obj = new Grass(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(32);
				ans.push_back(obj);
				isStatic = true;
			}
			if(mang_2_c[i][j] == 33)
			{
				obj = new Mountain(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(33);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 21)
			{
				//obj = new Brick(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj = new BrickEx(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 1);
				obj->SetID(21);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 22)
			{
				//CLand *cland = new CLand(_d3ddv, _backBuffer, _spriteHandler, j * 25.0f , i * 25.0f + 300);
				obj = new CLand(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(22);
				//cland->Init(_d3ddv, _backBuffer, _spriteHandler);
				//ans.push_back(cland);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 23)
			{
				obj = new Stone(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(23);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 24)
			{
				obj = new Pipe(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 24, 2);
				obj->SetID(24);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 25)
			{
				obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 25, 1);
				//obj->SetID(25);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 26)	//gach an
			{
				obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 26, 1);
				//obj->SetID(26);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 27)	//gach 5 coin
			{
				obj = new BrickEx(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 1);
				obj->SetID(27);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 28)	//gach sao
			{
				obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 28, 1);
				//obj->SetID(26);
				ans.push_back(obj);
				isStatic = true;
			}
			if(mang_2_c[i][j] == 29)	//gach sao
			{
				obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 29, 1);
				//obj->SetID(26);
				ans.push_back(obj);
				isStatic = true;
			}

			if(mang_2_c[i][j] == 11)
			{
				/*Enemy *enemy = new Enemy(j * 50.0f , i * 50.0f + 300);
				enemy->Init(_d3ddv, _backBuffer, _spriteHandler);
				ans.push_back(enemy);*/
				obj = new Goomba(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 2, 0);
				obj->SetID(11);
				ans.push_back(obj);
				isStatic = false;
			}

			if(mang_2_c[i][j] == 41)
			{
				obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 41);
				//obj->SetID(41);
				ans.push_back(obj);
				isStatic = true;	//object tinh
			}

			if(mang_2_c[i][j] == 43)		//nam mang
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 43, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}
			if(mang_2_c[i][j] == 431)		//nam mang
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 431, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}
			if(mang_2_c[i][j] == 432)		//nam mang
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 432, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}

			if(mang_2_c[i][j] == 45)		//nam lon + hoa
			{
				 
				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 45, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}

			if(mang_2_c[i][j] == 491)		//nam lon + hoa : save map
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 491, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}
			if(mang_2_c[i][j] == 492)		//nam lon + hoa : save map
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 492, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}
			if(mang_2_c[i][j] == 50)		//nam lon + hoa : save map
			{

				obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 50, 1, 0);
				//obj->SetID(45);
				ans.push_back(obj); 
				isStatic = false; //object dong, co the di chuyen
			}

			if(mang_2_c[i][j] == 46)
			{
				obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 46);
				//obj->SetID(41);
				ans.push_back(obj);
				isStatic = true;	//object tinh
			}
			if(mang_2_c[i][j] == 47)
			{
				obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 47);
				//obj->SetID(41);
				ans.push_back(obj);
				isStatic = true;	//object tinh
			}

			if(mang_2_c[i][j] == 2)
			{

				obj = new Mario(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, game_Mario, 2, 0);
				obj->SetID(2);
				ans.push_back(obj);
				isStatic = false; //object dong, co the di chuyen
			}
			/////////////////////////////////////


			if( mang_2_c[i][j] == 12)
			{
				/*Enemy *enemy = new Enemy(j * 50.0f , i * 50.0f + 300);
				enemy->Init(_d3ddv, _backBuffer, _spriteHandler);
				ans.push_back(enemy);*/

				//obj = new Enemy(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj = new Koopa(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 2, 0);
				//obj = new Background(_d3ddv, _backBuffer, _spriteHandler, j * 40.0f , i * 40.0f);
				//obj->Init(_d3ddv, _backBuffer, _spriteHandler);
				obj->SetID(12);
				ans.push_back(obj);
				isStatic = false;
			}

			 

			if(mang_2_c[i][j] == 44)
			{
				obj = new Star(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f, 44, 1, 0);
				ans.push_back(obj);
				isStatic = false;
			}

			/*if(mang_2_c[i][j] == 13)
			{
			obj = new LifeMushroom(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
			ans.push_back(obj);
			isStatic = true;
			}*/

			if(mang_2_c[i][j] == 42)
			{
				obj = new Pirhana(_d3ddv, _backBuffer, _spriteHandler, j * 32.0f , i * 32.0f);
				obj->SetID(42);
				ans.push_back(obj);
				isStatic = true;
			}

			if(obj!=NULL)
			{
				//obj->game=game;
				quadTree->Add(obj,isStatic);
			}
		}
		return ans;
}

void Map::SaveMapToFile(char* fileName, QuadTree *quadTree, MarioManager* game_Mario)
{

	ofstream fileStream(fileName);

	list<Object*>	staticObjs;
	list<Object*>	dynamicObjs;

	staticObjs.clear();
	dynamicObjs.clear();

	quadTree->GetObjectsAllNode(&staticObjs, &dynamicObjs);
	Object* obj;
	list<Object*>::iterator iList;

	fileStream<<width_map<<" "<<height_map<<"\n";

	/*for(iList = staticObjs.begin(); iList != staticObjs.end(); i++)
	{
	obj = *iList;
	if ((obj->GetX() >= j * 32.0f) && (obj->GetX() < (j+1) * 32.0f) && 
	(obj->GetY() >= i * 32.0f) && (obj->GetY() < (i+1) * 32.0f))
	{
	fileStream<<obj->GetID();
	}
	}
	fileStream<<staticObjs.size()<<" "<<height_map<<"\n";*/
	for (int i = 0; i < height_map; i++)
	{
		for (int j = 0; j < width_map; j++)
		{
			bool isZero = true;	//bien diem 0
			for(iList = staticObjs.begin(); iList != staticObjs.end(); iList++)
			{
				obj = *iList;
				if ((obj->GetX() >= j * 32.0f) && (obj->GetX() < (j+1) * 32.0f) && 
					(obj->GetY() >= i * 32.0f) && (obj->GetY() < (i+1) * 32.0f))
				{
					fileStream<<obj->GetID();
					isZero = false;
				}
			}

			for(iList = dynamicObjs.begin(); iList != dynamicObjs.end(); iList++)
			{
				obj=*iList;

				if ((obj->GetX() >= j * 32.0f) && (obj->GetX() < (j+1) * 32.0f) && 
					(obj->GetY() >= i * 32.0f) && (obj->GetY() < (i+1) * 32.0f))
				{
					fileStream<<obj->GetID();
					isZero = false;
				}

			}

			if (isZero == true)
			{
				fileStream<<0;
			}
			fileStream<<" ";
		}
		fileStream<<"\n";
	}

	fileStream.close(); //dong file
}


void Map::LoadMap(char* fileName, QuadTree *quadTree, MarioManager* game_Mario)
{
	 
	string data;
	ifstream fileStream(fileName);
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			data += fileStream.get();
		}
	}
	vector<string> dataSplit = split(data, '\n');
	string temp = dataSplit[0];
	vector<string> temp_vector = split(temp, ' ');
	
	width_map =  atoi(temp_vector[0].c_str());
	height_map =  atoi(temp_vector[1].c_str());

	numScore = atoi(temp_vector[2].c_str());
	numLife = atoi(temp_vector[3].c_str());
	numCoin = atoi(temp_vector[4].c_str());
	if (isLoadSubMap == false)
	{
		isLoadSubMap = atoi(temp_vector[5].c_str());
	} 
	isSubMap = atoi(temp_vector[6].c_str());		//mac dinh la 0, map phu = 1
	
	 
	// Da co width va height
	// Cap phat mang
	mang_2_c = (int**)malloc(height_map * sizeof(int*));
	for (int i = 0; i < height_map; i++)
		mang_2_c[i] = (int*)malloc(width_map * sizeof(int));
	for (int i = 0; i < height_map; i++)
	{
		vector<string> temp_splip = split(dataSplit[i + 1], ' ');
		for (int j = 0; j < width_map; j++)
		{
			mang_2_c[i][j] = atoi(temp_splip[j].c_str());
		}
	}
	// Luu mang Ok


	//Load map to quadtree
	Object *obj;
	bool isStatic;
	//quadTree = new QuadTree(0,0,720,0);

	vector<Object*> ans;
	for (int i = 0; i < height_map; i++)
	{	
		obj = NULL;
		if(mang_2_c[i][0] == 31)
		{
			obj = new CLoud(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(31);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 32)
		{
			obj = new Grass(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(32);
			ans.push_back(obj);
			isStatic = true;
		}
		if(mang_2_c[i][0] == 33)
		{
			obj = new Mountain(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(33);
			ans.push_back(obj);
			isStatic = false;
		}

		if(mang_2_c[i][0] == 21)
		{
			obj = new BrickEx(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], mang_2_c[i][1]);
			obj->SetID(21);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 22)
		{
			obj = new CLand(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(22);

			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 23)
		{
			obj = new Stone(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(23);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 24)
		{
			int state = mang_2_c[i][1];	//lay trang thai cua Pine

			if (isLoadSubMap == true && state == 3)		//trang thai danh dau load mario tu map phu toi map chinh theo vi tri cua Pine
			{
				if (saveMario->direction != 0)
				{
					obj = new Mario(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2], mang_2_c[i][3]+32, game_Mario, saveMario->object_State, saveMario->direction);
					obj->SetID(2);
					obj->ChangeState(MS_GOUPPINE);
					
					ans.push_back(obj);
					isStatic = false; //object dong, co the di chuyen
					

				}
				//else
				//{
				//	obj = new Mario(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2], mang_2_c[i][3], game_Mario, mang_2_c[i][1], mang_2_c[i][4]);
				//	obj->SetID(2);
				//	ans.push_back(obj);
				//	isStatic = false; //object dong, co the di chuyen
				//	obj->ChangeState(MS_GOUPPINE);
				//}

				if(obj!=NULL)
				{
					//obj->game=game;
					quadTree->Add(obj,isStatic);
				}
				 
			} 
			
			obj = new Pipe(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 24, mang_2_c[i][1]);
			//obj->SetID(24);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 25)
		{
			obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 25, mang_2_c[i][1]);
			//obj->SetID(25);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 26)	//gach an
		{
			obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 26, mang_2_c[i][1]);
			//obj->SetID(26);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 27)	//gach 5 coin
		{
			obj = new BrickEx(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], mang_2_c[i][1]);
			obj->SetID(27);
			ans.push_back(obj);
			isStatic = true;
		}

		if(mang_2_c[i][0] == 28)	//gach sao
		{
			obj = new BrickQuestion(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 28, mang_2_c[i][1]);
			//obj->SetID(26);
			ans.push_back(obj);
			isStatic = true;
		}
		 
		if(mang_2_c[i][0] == 29)
		{
			 
			obj = new Pipe(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 29, mang_2_c[i][1]);
			//obj->SetID(24);
			ans.push_back(obj);
			isStatic = true;
		}
		

		if(mang_2_c[i][0] == 41)
		{
			obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 41);
			//obj->SetID(41);
			ans.push_back(obj);
			isStatic = true;	//object tinh
		}
		if(mang_2_c[i][0] == 42)
		{
			obj = new Pirhana(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(42);
			ans.push_back(obj);
			isStatic = true;
		}
		
		if(mang_2_c[i][0] == 43)		//nam mang
		{

			obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 43, mang_2_c[i][1], mang_2_c[i][4]);
			//obj->SetID(45);
			ans.push_back(obj); 
			isStatic = false; //object dong, co the di chuyen
		}
		 
		if(mang_2_c[i][0] == 44)
		{
			obj = new Star(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 44, mang_2_c[i][1], mang_2_c[i][4]);
			ans.push_back(obj);
			isStatic = false;
		} 

		if(mang_2_c[i][0] == 45)		//nam lon + hoa
		{

			obj = new PowerUp(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 45, mang_2_c[i][1], mang_2_c[i][4]);
			//obj->SetID(45);
			ans.push_back(obj); 
			isStatic = false; //object dong, co the di chuyen
		}

		 
		 

		if(mang_2_c[i][0] == 46)
		{
			obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 46);
			//obj->SetID(41);
			ans.push_back(obj);
			isStatic = true;	//object tinh
		}
		if(mang_2_c[i][0] == 47)
		{
			obj = new Coin(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], 47);
			//obj->SetID(41);
			ans.push_back(obj);
			isStatic = true;	//object tinh
		}

		if(mang_2_c[i][0] == 2)
		{
			if (isLoadSubMap == false || isSubMap == true)
			{
				if (saveMario->direction != 0)
				{
					obj = new Mario(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2], mang_2_c[i][3], game_Mario, saveMario->object_State, saveMario->direction);
					obj->SetID(2);
					ans.push_back(obj);
					isStatic = false; //object dong, co the di chuyen

					
				} 
				else
				{
					obj = new Mario(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], game_Mario, mang_2_c[i][1], mang_2_c[i][4]);
					obj->SetID(2);
					ans.push_back(obj);
					isStatic = false; //object dong, co the di chuyen
				}

			}
			
		}
		/////////////////////////////////////
		if(mang_2_c[i][0] == 11)
		{

			obj = new Goomba(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], mang_2_c[i][1], mang_2_c[i][4]);
			obj->SetID(11);
			ans.push_back(obj);
			isStatic = false;
		}

		if( mang_2_c[i][0] == 12)
		{
			obj = new Koopa(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3], mang_2_c[i][1], mang_2_c[i][4]);
			obj->SetID(12);
			ans.push_back(obj);
			isStatic = false;
		}
		
		if( mang_2_c[i][0] == 51)
		{
			obj = new CheckPoint(_d3ddv, _backBuffer, _spriteHandler, mang_2_c[i][2] , mang_2_c[i][3]);
			obj->SetID(51);
			ans.push_back(obj);
			isStatic = true;
		}
		

		if(obj!=NULL)
		{
			//obj->game=game;
			quadTree->Add(obj,isStatic);
		}
	}
	//return ans;
}
 

void Map::SaveMap(char* fileName, QuadTree *quadTree, MarioManager* game_Mario)
{
	ofstream fileStream(fileName);

	list<Object*>	staticObjs;
	list<Object*>	dynamicObjs;

	staticObjs.clear();
	dynamicObjs.clear();

	quadTree->GetObjectsAllNode(&staticObjs, &dynamicObjs);
	Object* obj;
	list<Object*>::iterator iList;

	height_map = staticObjs.size() + dynamicObjs.size();
	fileStream<<width_map<<" "<<height_map<<" "<<numScore<<" "<<numLife<<" "<<numCoin<<" "<<isLoadSubMap<<" "<<isSubMap<<"\n";
  	 
	for(iList = staticObjs.begin(); iList != staticObjs.end(); iList++)
	{
		obj = *iList;
		fileStream<<obj->GetID()<<" "<<obj->object_State<<" "<<obj->GetX()<<" "<<obj->GetY()<<" "<<obj->direction<<"\n";
	}

	for(iList = dynamicObjs.begin(); iList != dynamicObjs.end(); iList++)
	{
		obj=*iList;
		fileStream<<obj->GetID()<<" "<<obj->object_State<<" "<<obj->GetX()<<" "<<obj->GetY()<<" "<<obj->direction<<"\n";
	}

	fileStream.close(); //dong file
}